layui.use(['table','layer','form'],function(){
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;
    var form = layui.form;

    /**
     * 头部⼯具栏事件
     */
    table.on("toolbar(users)", function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case "add":
                openAddOrUpdateReasonDialog();
                break;
            case "del":
                deleteUser(checkStatus.data);
                break;
        }
    });
    /**
     * 批量删除用户
     * @param datas
     */
    function deleteUser(datas) {
        if(datas.length == 0){
            layer.msg("请选择删除记录!", {icon: 5});
            return;
        }
        layer.confirm('确定删除选中的用户记录？', {
            btn: ['确定','取消'] //按钮
        }, function(index){
            layer.close(index);
            var ids= "ids=";
            for(var i=0;i<datas.length;i++){
                if(i<datas.length-1){
                    ids=ids+datas[i].id+"&ids=";
                }else {
                    ids=ids+datas[i].id
                }
            }
            $.ajax({
                type:"post",
                url:ctx + "/ro/deleteMore",
                data:ids,
                dataType:"json",
                success:function (data) {
                    if(data.code==200){
                        tableIns.reload();
                    }else{
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            })
        });
    }

    /**
     * ⾏监听事件
     */
    table.on("tool(users)", function(obj){
        var layEvent = obj.event;
        // 监听编辑事件
        if(layEvent === "edit") {
            openAddOrUpdateReasonDialog(obj.data.id);
        } else if(layEvent === "del") {
            // 监听删除事件
            layer.confirm('确定删除当前原因？', {icon: 3, title: "原因管理"}, function (index) {
                $.post(ctx + "/ro/delete",{ids:obj.data.id},function (data) {
                    if(data.code==200){
                        layer.msg("操作成功！");
                        tableIns.reload();
                    }else{
                        layer.msg(data.msg, {icon: 5});
                    }
                });
            });
        }
    });
    /**
     * 可用开关状态更新
     */
    form.on("switch(switchDefault)",function (obj){
        console.log(obj)
        var isAvailable = obj.elem.checked?1:0;
        //方法一取数据（根据相对位置取）
        var id = obj.elem.attributes['reasonid'].nodeValue;
        var url = "ro/update"
        $.ajax({
            type:"post",
            url:ctx + "/ro/update?isAvailable="+isAvailable+"&id="+id,
            dataType:"json",
            success:function (data) {
                if(data.code==200){
                    tableIns.reload();
                }else{
                    layer.msg(data.msg, {icon: 5});
                }
            }
        })
    })

    /**
     * 打开用户添加或更新对话框
     */
    function openAddOrUpdateReasonDialog(reasonId) {
        var url  =  ctx + "/ro/addOrUpdateReasonPage";
        var title = "用户管理-用户添加";
        if(reasonId){
            url = url + "?id="+reasonId;
            title = "用户管理-用户更新";
        }
        layui.layer.open({
            title : title,
            type : 2,
            area:["550px","300px"],
            maxmin:true,
            content : url
        });
    }


    /**
     * 用户列表展示
     */
    var  tableIns = table.render({
        elem: '#roList',
        url : ctx+'/ro/list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 10,
        toolbar: "#toolbarDemo",
        id : "roListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50},
            {field: "id", title:'编号',fixed:"true", width:80},
            {field: 'reason', title: '原因类型', minWidth:50, align:"center"},
            {field: 'od', title: '排序', minWidth:100, align:'center'},
            {
                field: 'isAvailable', title: '是否可用', align: 'center', templet: function (d) {
                    return formatterIsAvailable(d.isAvailable,d.id);
                }
            },
            {title: '操作', minWidth:150, templet:'#roListBar',fixed:"right",align:"center"}
        ]]
    });
    function formatterIsAvailable(isAvailable,id) {
        if (isAvailable == 0) {
            return '<input id="isAvailable" reasonId='+id+' data-id="1" lay-filter="switchDefault" type="checkbox" name="isAvailable"  lay-skin="switch">';
        } else if (isAvailable == 1) {
            return '<input id="isAvailable" reasonId='+id+' data-id="1" lay-filter="switchDefault" type="checkbox" name="isAvailable" checked  lay-skin="switch">';
        }
    }
});